package model

import "time"

type Customer struct {
	CustomerId int `xorm:"pk autoincr"`
	CustomerName string
	CustomerType int
	NewCustomerType int
	FounderId int
	FounderName string
	BelongerId int
	BelongerName string
	AllocationTime time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Order struct {
	OrderId int `xorm:"pk autoincr"`
	OrderType int
	OrderState int
	CustomerId int
	CustomerName string
	NextSinglePersonId int
	NextSinglePersonName string
	MealList string
	TotalPrice int64
	Region string
	CreatedAt time.Time
	UpdatedAt time.Time
}

func JudgeCustomerType()(map[string]interface{}, error){

	var cList []*Customer
	x.Find(&cList)

	for _, cv := range cList{
		consumptionAmount, err := x.Where("customer_id = ?", cv.CustomerId).NotIn("order_state", []int{4, 22, 40, 91}).SumInt(new(Order), "total_price")
		if err != nil{
			return nil ,err
		}

		purchaseFrequency, err := x.Where("customer_id = ?", cv.CustomerId).NotIn("order_state", []int{4, 22, 40, 91}).Count(new(Order))
		if err != nil{
			return nil ,err
		}

		var firstOrder Order
		x.Where("customer_id = ?", cv.CustomerId).NotIn("order_state", []int{4, 22, 40, 91}).Desc("created_at").Limit(1).Get(&firstOrder)
		consumptionTime := int(time.Now().Sub(firstOrder.CreatedAt).Hours()/24)

		if consumptionAmount >= 50000{
			cv.NewCustomerType = 1
		}

		if consumptionTime <= 60 && purchaseFrequency >= 4 || (consumptionTime <= 60 && (consumptionAmount < 50000 && consumptionAmount >= 10000)) || (purchaseFrequency >= 4 && (consumptionAmount < 50000 && consumptionAmount >= 10000)){
			cv.NewCustomerType = 2
		}else{
			if consumptionAmount < 50000 && consumptionAmount >= 10000 {
				cv.NewCustomerType = 2
			}
		}

		if (consumptionTime > 60 && consumptionTime <= 120) && (purchaseFrequency >= 3 && purchaseFrequency < 4) || ((consumptionTime > 60 && consumptionTime <= 120) && (consumptionAmount < 10000 && consumptionAmount >= 5000)) || ((purchaseFrequency >= 3 && purchaseFrequency < 4) && (consumptionAmount < 10000 && consumptionAmount >= 5000)){
			cv.NewCustomerType = 3
		}else {
			if consumptionAmount < 10000 && consumptionAmount >= 5000 {
				cv.NewCustomerType = 3
			}
		}

		if (consumptionTime > 120 && consumptionTime <= 180) && (purchaseFrequency >= 1 && purchaseFrequency < 3) || ((consumptionTime > 120 && consumptionTime <= 180) && (consumptionAmount < 5000 && consumptionAmount >= 3000)) || ((purchaseFrequency >= 1 && purchaseFrequency < 3) && (consumptionAmount < 5000 && consumptionAmount >= 3000)){
			cv.NewCustomerType = 4
		}else {
			if consumptionAmount < 5000 && consumptionAmount >= 3000 {
				cv.NewCustomerType = 4
			}
		}

		if consumptionTime > 180 && (purchaseFrequency >= 0 && purchaseFrequency < 1) || (consumptionTime > 180 && consumptionAmount < 3000 ) || ((purchaseFrequency >= 0 && purchaseFrequency < 1) && consumptionAmount < 3000){
			cv.NewCustomerType = 5
		}else {
			if consumptionAmount < 3000{
				cv.NewCustomerType = 5
			}
		}

		x.Id(cv.CustomerId).Update(cv)
	}

	return map[string]interface{}{"code":200, "message":"成功"}, nil
}

type ShareDetails struct {
	Id int64 `xorm:"pk autoincr"`
	Type int
	OrderId int64
	PerformanceOwber string
	SharingObject string
	SharedAmount int64
	Operator string
}

type Acount struct {
	DepartMentName string
	SharedObject string
	ShareRatio float64
	SharedAmount int64
}

func Sharing(oId int64, aList []*Acount, t int)(map[string]interface{}, error){

	var o Order
	x.Id(oId).Get(&o)

	for _, a := range aList{
		var sd = ShareDetails{
			OrderId:          oId,
			Type:             t,
			PerformanceOwber: o.NextSinglePersonName,
			SharingObject:    a.SharedObject,
			SharedAmount:     a.SharedAmount,
		}
		x.Insert(&sd)
	}

	if t == 1{

	}else if t == 2{

	}else if t == 3{

	}

	return map[string]interface{}{"code":200, "message":"成功"}, nil
}